package pt.compflow.client.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.restlet.data.MediaType;

import pt.compflow.Context;
import pt.compflow.client.BaseResource;
import pt.compflow.client.TemplateOutputRepresentation;
import pt.compflow.flowontology.model.FlowOntologyModel;
import pt.compflow.flowontology.model.Person;
import pt.compflow.flowontology.model.PersonGroupMembership;
import pt.compflow.flowontology.model.TaskInterface;

import com.hp.hpl.jena.util.iterator.ExtendedIterator;

import freemarker.template.Configuration;
import freemarker.template.TemplateExceptionHandler;
import freemarker.template.Version;

public abstract class UIBaseResource extends BaseResource {

	protected boolean isAdmin(Person p) {
		boolean isAdmin = false;
		Iterator<PersonGroupMembership> memIt = p.listHasMembershipPersonGroupMemberships();
		while(!isAdmin && memIt.hasNext()) {
			PersonGroupMembership mem = memIt.next();
			if(mem.getHasGroupPersonGroup().getURI().compareTo(ADMIN_LOCAL_GROUP_URI) == 0)
				isAdmin = true;
		}
		return isAdmin;
	}
	
	protected Map<String, Object> getPersonMap(FlowOntologyModel model, Person p, boolean simpleData) {
		
		Map<String, Object> argumentMap = new HashMap<String, Object>();
		argumentMap.put("firstName", p.getHasFirstName(null));
		argumentMap.put("lastName", p.getHasLastName(null));
		argumentMap.put("mail", p.getHasEmail(null));
		argumentMap.put("registryTime", p.getRegistryTime().getDateTimeString());
		argumentMap.put("isAdmin", isAdmin(p));
		
		// worker stuff		
		List<Map<String, Object>> interfaceList = new ArrayList<Map<String,Object>>();
		ExtendedIterator<TaskInterface> it2 = p.listAccessibleThroughTaskInterfaces();
		while(it2.hasNext()) {
			TaskInterface interfaze = it2.next();
			Map<String, Object> iMap = new HashMap<String, Object>();
			iMap.put("shortUri", interfaze.getLocalName());
			iMap.put("uri", interfaze.getURI());
			iMap.put("label", interfaze.getLabel(null));
			interfaceList.add(iMap);
		}
		argumentMap.put("interfaces", interfaceList);
		
		if(!simpleData) {
			List<Map<String, Object>> membershipList = new ArrayList<Map<String,Object>>();
			ExtendedIterator<PersonGroupMembership> it = p.listHasMembershipPersonGroupMemberships();
			while(it.hasNext()) {
				PersonGroupMembership membership = it.next();
				Map<String, Object> membershipMap = new HashMap<String, Object>();
				membershipMap.put("uri", membership.getHasGroupPersonGroup().getURI());
				membershipMap.put("label", membership.getHasGroupPersonGroup().getLabel(null));
				membershipList.add(membershipMap);
			}
			argumentMap.put("memberships", membershipList);
		}
		
		return argumentMap;
	}
	
	protected Configuration getTemplateConfiguration() {
		Configuration cfg = new Configuration();
	    
	    // Where do we load the templates from:
	    cfg.setClassForTemplateLoading(Context.class, "/");
	    
	    // Some other recommended settings:
	    cfg.setIncompatibleImprovements(new Version(2, 3, 20));
	    cfg.setDefaultEncoding("UTF-8");
	    //cfg.setLocale(Locale.US);
	    cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
	    
	    return cfg;
	}
	
	protected TemplateOutputRepresentation getTemplateRepresentation(String templateFile, Map<String, Object> attributes) {
		return new TemplateOutputRepresentation(getTemplateConfiguration(), MediaType.TEXT_HTML, templateFile, attributes);
	}
}
